package com.example.hasee.zhcd.Activities.ChuanWu;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Activities.XiaoXi.MessageActivity;
import com.example.hasee.zhcd.Adapters.ArrivalsListAdapter;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by hasee on 2018/5/28.
 */

public class ArrivalsActivity extends BaseActivity {
    private TextView title;
    private ImageView back;
    private ImageView home;
    private ListView arrivals_list;
    private HashMap<String, String> map = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private String checkedString = "0";
    private ArrivalsListAdapter adapter;
    private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    private ArrayList<String> spnamelist = new ArrayList<String>();
    private ArrayList<String> spcodelist = new ArrayList<String>();
    private String spcodeString;
    private ArrayAdapter<String> spadapter;
    private Spinner qkspinner;
    private TextView account;
    private EditText shipname;
    private CheckBox zd_ship;
    private String strShip, strZD;
    private Button btn_query;

    @Override
    public int contentViewId() {
        return R.layout.activity_arrivals;
    }

    @Override
    public void set() {
        title.setText("预抵及锚地船舶");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
        btn_query.setOnClickListener(this);
        zd_ship.setChecked(false);
        strZD = "0";
        zd_ship.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    strZD = "1";
                } else {
                    strZD = "0";
                }
                strShip = shipname.getText().toString().trim();
                getArrivalsList();
            }
        });
        getQueueKind("预抵及锚地");
    }

    @Override
    public void init() {
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        arrivals_list = (ListView) findViewById(R.id.arrivals_list);
        qkspinner = (Spinner) findViewById(R.id.qkspinner);
        account = (TextView) findViewById(R.id.account);
        shipname = (EditText) findViewById(R.id.ship);
        zd_ship = (CheckBox) findViewById(R.id.ship_check);
        btn_query = (Button) findViewById(R.id.btn_query);
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {

            case "Fail":
                ProgressDialogUtils.closeProgressDialog();
                Toast.makeText(ArrivalsActivity.this, "操作失败，请检查网络！", Toast.LENGTH_LONG).show();
                break;
            case "YWCX_ydcb_order":
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        for (int i = 0; i < data.length(); i++) {
                            spnamelist.add(data.getJSONObject(i).getString("fname"));
                            spcodelist.add(data.getJSONObject(i).getString("fcode"));
                        }
                        spadapter = new ArrayAdapter<String>(ArrivalsActivity.this, android.R.layout.simple_spinner_item, spnamelist);
                        spadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        qkspinner.setAdapter(spadapter);
                        spcodeString = spcodelist.get(0);
                        qkspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                spcodeString = spcodelist.get(position);
                                strShip = shipname.getText().toString().trim();
                                getArrivalsList();
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });

                    } else {
                        String rspmessage = json.getString("rspmessage");
                        Toast.makeText(ArrivalsActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "ZHCW_ydcbList":
                ProgressDialogUtils.closeProgressDialog();
                //   Log.e("ZHCW_ydcbList",msg.getMsg());
                //{"fshipname":"国裕15号", "fshipcode":"10666", "fshipordernum":"1824", "fzygs":"", "fberth":"",
                // "fyddate":"06-01 00:55", "fcgs":"wldw_12017", "fwbsx":"", "fshipyearnumvcr":"QHD201802372"}
                try {
                    Log.e("strZD", strZD);
                    Log.e("ZHCW_ydcbList", msg.getMsg());
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    account.setText(rspmessage);
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        list.clear();
                        for (int i = 0; i < data.length(); i++) {
                            HashMap<String, String> smap = new HashMap<String, String>();
                            smap.put("fshipname", data.getJSONObject(i).getString("fshipname"));
                            smap.put("fshipcode", data.getJSONObject(i).getString("fshipcode"));
                            smap.put("fshipordernum", data.getJSONObject(i).getString("fshipordernum"));
                            smap.put("fzygs", data.getJSONObject(i).getString("fzygs"));
                            smap.put("fyddate", data.getJSONObject(i).getString("fyddate"));
                            smap.put("fshipyearnumvcr", data.getJSONObject(i).getString("fshipyearnumvcr"));
                            smap.put("fcgs", data.getJSONObject(i).getString("fcgs"));
                            smap.put("fberth", data.getJSONObject(i).getString("fberth"));
                            smap.put("fwbsx", data.getJSONObject(i).getString("fwbsx"));
                            list.add(smap);
                        }
                        adapter = new ArrivalsListAdapter(ArrivalsActivity.this, list);
                        arrivals_list.setAdapter(adapter);
                    } else {
                        String rspmessage1 = json.getString("rspmessage");
                        Toast.makeText(ArrivalsActivity.this, rspmessage1, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void getQueueKind(String strPageName) {
        ProgressDialogUtils.createProgressDialog(ArrivalsActivity.this, "正在获取列表...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strPageName", strPageName);
        httpUtils.get("YWCX_ydcb_order", map, "YWCX_ydcb_order");
    }

    private void getArrivalsList() {
        ProgressDialogUtils.createProgressDialog(ArrivalsActivity.this, "正在获取列表...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strOrder", spcodeString);
        map.put("strShip", strShip);
        map.put("strZD", strZD);
        httpUtils.get("ZHCW_ydcbList", map, "ZHCW_ydcbList");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.btn_query:
                strShip = shipname.getText().toString().trim();
                getArrivalsList();
                break;
        }
    }
}
